package BuildTrainSet.Initial;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author phamthanh
 */
public class Split {

    public Split(String inF, String ouF) throws XMLStreamException, FileNotFoundException, IOException {
        long startTime = System.currentTimeMillis();
        int com=0;
        int point=0;

        System.out.println("package BuildTrainSet.Initial; class Split");

        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        FileOutputStream fos = new FileOutputStream(ouF);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);

        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);
                    }

                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();

                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();

                    }
                    if (localName.equals("Infomation")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }

                    if (localName.equals("W")) {
                        String attributeName = xmlStreamReader.getAttributeLocalName(0);
                        String attrbuteValue = xmlStreamReader.getAttributeValue(0);
                        if (!attrbuteValue.equals("")) {
                            String nsw = xmlStreamReader.getElementText();
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute(attributeName, attrbuteValue);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        } else {
                            String nsw = xmlStreamReader.getElementText();
                            ArrayList ar = new ArrayList();
                            splitNSW(nsw, ar);
                            if (!isCompound(nsw)) {
                                xmlStreamWriter.writeStartElement(localName);
                                xmlStreamWriter.writeCharacters(nsw);

                            } else {
                                com++;
                                xmlStreamWriter.writeStartElement("SPLIT");
                                for (int i = 0; i < ar.size(); i++) {
                                    xmlStreamWriter.writeStartElement("W");
                              //      xmlStreamWriter.writeAttribute("NSW", "");
                                    xmlStreamWriter.writeCharacters((String) ar.get(i));
                                    xmlStreamWriter.writeEndElement();
                                    point++;
                                }
                                    point--;
                            }
                            xmlStreamWriter.writeEndElement();
                        }

                    }


                    break;
                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();
                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();

                    }
                    if (endLocalName.equals("Infomation")) {
                        xmlStreamWriter.writeEndElement();
                    }



                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    xmlStreamWriter.writeCharacters(character);
                    break;

                default:
                    break;
            }
        }


        xmlStreamWriter.writeEndElement();
        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();
        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("Thoi gian de split la: " + time);
        System.out.println("    +com: "+com);
         System.out.println("    +point: "+point);

    }

    public void splitNSW(String nsw, ArrayList ar) {
        Pattern SPLIT = Pattern.compile("((\\p{L}\\p{N})|(\\p{N}\\p{L})|([\\p{P}\\+]\\p{L})|(\\p{L}[\\p{P}\\+])|(\\d[\\p{P}\\+&&[^,\\.]])|([\\p{P}\\+&&[^,\\.]]\\d)|(\\p{Ll}\\p{Lu}))");

        Matcher m = SPLIT.matcher(nsw);
        int lastIndex = 0;
        if (m.find()) {
            String sNsw = nsw.substring(lastIndex, m.start() + 1);
            ar.add(sNsw);
            nsw = nsw.substring(m.end() - 1);
            splitNSW(nsw, ar);
        } else {
            ar.add(nsw);
        }

    }

    public boolean isCompound(String nsw) {
        Pattern COMPOUND = Pattern.compile("((\\p{L}\\p{N})|(\\p{N}\\p{L})|([\\p{P}\\+]\\p{L})|(\\p{L}[\\p{P}\\+])|(\\p{Ll}\\p{Lu}))");
        Matcher m = COMPOUND.matcher(nsw);
        return m.find();
    }
}
